<?php
/*
 * @Author: lokei
 * @Date: 2022-08-31 15:47:43
 * @LastEditors: lokei
 * @LastEditTime: 2022-12-24 20:45:02
 * @Description: 
 */
namespace App\Http\Controllers\Iot\Device;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Iot\Device\DeviceModel;
use Illuminate\Http\Request;

class DeviceController extends Controller {
    public function list() {
        $items = DeviceModel::get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function ofStore(Request $request) {
        $items = DeviceModel::where('store_id', '=', $request->input('store_id'))->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function ofSim() {
        $items = DeviceModel::where('type', '=', '0')->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function get(Request $request) {
        $device = DeviceModel::where('id', '=', $request->input('id'))->first();
        $res = ResultTool::success();
        $res['data'] = $device;
        return $res;
    }

    public function status(Request $request) {
        $device = DeviceModel::where('id', '=', $request->input('id'))->first();
        $res = ResultTool::success();
        $res['data']['status'] = $device->status;
        $res['data']['last_on'] = $device->last_on;
        $res['data']['last_duration'] = $device->last_duration;
        return $res;
    }

    public function create(Request $request) {
        $exist_device = DeviceModel::where('identity', '=', $request->input('identity'))->first();
        if(is_null($exist_device)) {
            $device = new DeviceModel();
            $device->store_id = $request->input('store_id');
            $device->identity = $request->input('identity');
            $device->name = $request->input('name');
            $device->type = $request->input('type');
            $device->save();
            return ResultTool::success();
        } else {
            $res = ResultTool::fail();
            $res['errorMsg'] = '设备标识已存在！';
            return $res;
        }
    }

    public function update(Request $request) {
        $device = DeviceModel::where('id', '=', $request->input('id'))->first();
        $device->store_id = $request->input('store_id');
        $device->identity = $request->input('identity');
        $device->name = $request->input('name');
        $device->type = $request->input('type');
        $device->save();
        return ResultTool::success();
    }

    public function setGateway(Request $request) {
        $device = DeviceModel::where('id', '=', $request->input('id'))->first();
        $device->gateway_id = $request->input('gateway_id');
        $device->save();
        return ResultTool::success();
    }

    public function delete(Request $request) {
        DeviceModel::where('id', '=', $request->input('id'))->delete();
        return ResultTool::success();
    }
}